package com.ruoyi.biz.controller;

import com.ruoyi.collect.domain.Categories;
import com.ruoyi.collect.domain.CookTimeOptions;
import com.ruoyi.collect.domain.DifficultyOptions;
import com.ruoyi.collect.domain.CollectRecipes;
import com.ruoyi.collect.service.ICategoriesService;
import com.ruoyi.collect.service.ICookTimeOptionsService;
import com.ruoyi.collect.service.IDifficultyOptionsService;
import com.ruoyi.collect.service.IRecipesService;
import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 菜谱管理Controller
 * 
 * @author lpt
 * @date 2025-08-14
 */
@Anonymous
@RestController("recipeCollectionItemsController")
@RequestMapping("/collect/recipes")
public class RecipesController extends BaseController
{
    @Autowired
    private IRecipesService recipesService;
    
    @Autowired
    private ICookTimeOptionsService cookTimeOptionsService;
    
    @Autowired
    private IDifficultyOptionsService difficultyOptionsService;
    
    @Autowired
    private ICategoriesService categoriesService;

    /**
     * 查询菜谱管理列表
     */
    @Anonymous
    @GetMapping("/list")
    public TableDataInfo list(CollectRecipes recipes)
    {
        startPage();
        List<CollectRecipes> list = recipesService.selectRecipesList(recipes);
        return getDataTable(list);
    }

    /**
     * 导出菜谱管理列表
     */
    @PreAuthorize("@ss.hasPermi('collect:recipes:export')")
    @Log(title = "菜谱管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CollectRecipes recipes)
    {
        List<CollectRecipes> list = recipesService.selectRecipesList(recipes);
        ExcelUtil<CollectRecipes> util = new ExcelUtil<CollectRecipes>(CollectRecipes.class);
        util.exportExcel(response, list, "菜谱管理数据");
    }

    /**
     * 获取菜谱管理详细信息
     */
    @Anonymous
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return success(recipesService.selectRecipesById(id));
    }

    /**
     * 新增菜谱管理
     */
    @Log(title = "菜谱管理", businessType = BusinessType.INSERT)
    @Anonymous
    @PostMapping
    public AjaxResult add(@RequestBody CollectRecipes recipes)
    {
        return toAjax(recipesService.insertRecipes(recipes));
    }

    /**
     * 修改菜谱管理
     */
    @Log(title = "菜谱管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CollectRecipes recipes)
    {
        return toAjax(recipesService.updateRecipes(recipes));
    }

    /**
     * 删除菜谱管理
     */
    @PreAuthorize("@ss.hasPermi('collect:recipes:remove')")
    @Log(title = "菜谱管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids)
    {
        return toAjax(recipesService.deleteRecipesByIds(ids));
    }


    //--------------------------------------------------------------------
    @Anonymous
    @GetMapping("/select/{id}")
    public CollectRecipes selectById(@PathVariable("id") String id){
        return recipesService.findById(id);
    }

    @Anonymous
    @GetMapping("/select")
    public List<CollectRecipes> select(){
        return recipesService.findBatch();
    }

    @Anonymous
    @GetMapping("/select/page")
     public Object findPage(@RequestParam(value = "pageNum", required = false, defaultValue = "1") Integer pageNum,
                           @RequestParam(value = "pageSize", required = false, defaultValue = "2") Integer pageSize,
                            CollectRecipes recipes) {
        return recipesService.findByPage(pageNum, pageSize, recipes);
    }

    @Anonymous
    @DeleteMapping("/delete")
    public String delete(@RequestBody Integer[] ids) {
        return recipesService.removeBatch(ids)> 0 ? "批量删除成功" : "批量删除失败";
    }

    @Anonymous
    @DeleteMapping("/delete/{id}")
    public String delete(@PathVariable Integer id) {
        return recipesService.remove(id) > 0 ? "删除成功" : "删除失败";
    }

    @Anonymous
    @PostMapping("/insert")
    public String addid(@RequestBody CollectRecipes recipes) {
        return recipesService.addid(recipes) > 0 ? "新增成功" : "新增失败";
    }
    
    @Anonymous
    @GetMapping("/test")
    public String test() {
        return "匿名访问测试成功！";
    }

    //****************************************************
    @Anonymous
    @PutMapping("/update")
    public Integer update(@RequestBody CollectRecipes recipes) {
        return recipesService.editid(recipes);
    }

    /**
     * 获取烹饪时间选项列表
     */
    @Anonymous
    @GetMapping("/cook-time-options")
    public AjaxResult getCookTimeOptions() {
        List<CookTimeOptions> list = cookTimeOptionsService.selectCookTimeOptionsList(new CookTimeOptions());
        return success(list);
    }

    /**
     * 获取难度选项列表
     */
    @Anonymous
    @GetMapping("/difficulty-options")
    public AjaxResult getDifficultyOptions() {
        List<DifficultyOptions> list = difficultyOptionsService.selectDifficultyOptionsList(new DifficultyOptions());
        return success(list);
    }

    /**
     * 获取分类列表
     */
    @Anonymous
    @GetMapping("/categories")
    public AjaxResult getCategories() {
        List<Categories> list = categoriesService.selectCategoriesList(new Categories());
        return success(list);
    }

}
